#!/usr/bin/env node

// ./02-get-repos.js su-ziyi1
const https = require('https');

// 获取命令行参数
const args = process.argv.slice(2);
if (args.length === 0) {
    console.log('Usage: node 02-get-repos.js <username>');
    process.exit(1);
}

const username = args[0];

// Gitee API URL
const url = `https://gitee.com/api/v5/users/${username}/repos`;

https.get(url, (res) => {
    let data = '';

    res.on('data', (chunk) => {
        data += chunk;
    });

    res.on('end', () => {
        try {
            const repos = JSON.parse(data);
            console.log(`Total: ${repos.length}`);
            repos.forEach(repo => {
                console.log(repo.name);
            });
        } catch (error) {
            console.error('Failed to fetch repositories:', error);
        }
    });
}).on('error', (e) => {
    console.error(`Got error: ${e.message}`);
});